// index.js
var ports = require("../../utils/ports.js")
// 获取应用实例
const app = getApp()

Component({
  data: {
    botH: '0px',
    //background: ['../../img/slideimg.jpg', '../../img/pro1.jpg', '../../img/pro2.jpg'],
    indcolor: '#FFFFFF',
    indactcolor: '#6B162B',
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    noticelist: [
      { nimgsrc: '../../img/voice-r.png', words: '配送5元起，满72元可领取优惠券三公里内包邮，满128元可减8元' },
      { nimgsrc: '../../img/voice-w.png', words: '配送5元起，满72元可领取优惠券三公里内包邮，满128元可减8元' },
    ],
    bonusDefineList: [],//红包列表
    showpromodel: false,
  },
  attached: function () {
    var that = this
    that.setData({
      botH: app.globalData.bottomHeight + 'px',
      thisBotH: 100 + app.globalData.bottomHeight + 'rpx'
    })
    //console.log(app.globalData.bottomHeight);    
    that.load()
  },
  methods: {
    enterAni() {
      var that = this
      //进入动画
      var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease',
        delay: 0
      });
      animation.opacity(1).translateY(0).step()
      that.setData({
        ani: animation.export()
      })
    },
    outAni() {
      var that = this
      //清除动画
      var animation = wx.createAnimation({
        duration: 10,
        timingFunction: 'ease',
        delay: 0
      });
      animation.opacity(0.2).translateY(30).step()
      that.setData({
        ani: animation.export()
      })
    },
    //搜索
    bindsearch: function () {
      wx.navigateTo({
        url: '/pages/searchgood/searchgood'
      })
    },
    /* 图片加载设置swiper */
    loadSwiperImg: function (event) {
      var flag = event.detail.height * 375 * 2 / event.detail.width;
      this.setData({
        swiperimg: this.data.swiperimg > flag ? this.data.swiperimg : flag
      })
    },
    load() {
      var that = this
      //轮播
      ports.ModuleFocus.getFocusPictureList({
        focusID: app.focusID,
        sortTypeOrder: 1
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            focusList: res.data.body.data.rows
          })
        }
      })
      //首页的滚动公告
      ports.ModuleArticle.getNavigatorReleaseList({
        navigatorID: app.hotNavId,
        objectDefineID: app.articleobjectDefineID
      }, that, res => {
        var newsList = res.data.body.data.rows;
        this.setData({
          newsList: newsList
        })
      })
      //首页 新品上市
      ports.ModuleArticle.getNavigatorReleaseList({
        navigatorID: app.hotRecommandNavId,
        pageNumber: 2,
        objectDefineID: app.goodsobjectDefineID
      }, that, res => {
        var HotGoodsList = res.data.body.data.rows;
        //console.log(HotGoodsList)
        this.setData({
          HotGoodsList: HotGoodsList
        })
      })
      //首页 招牌系列
      ports.ModuleArticle.getNavigatorReleaseList({
        navigatorID: app.zhaopaiId,
        pageNumber: 4,
        objectDefineID: app.goodsobjectDefineID
      }, that, res => {
        var zpGoodsList = res.data.body.data.rows;
        this.setData({
          zpGoodsListfirst: zpGoodsList[0],//第一个,
          zpGoodsListOthers: zpGoodsList.slice(1, zpGoodsList.length)//剩下的
        })
      })
      //首页 主厨推荐
      ports.ModuleArticle.getNavigatorReleaseList({
        navigatorID: app.zhuchuTjId,
        pageNumber: 4,
        objectDefineID: app.goodsobjectDefineID
      }, that, res => {
        var zctjGoodsList = res.data.body.data.rows;
        this.setData({
          zctjGoodsListfirst: zctjGoodsList[0],//第一个,
          zctjGoodsListOthers: zctjGoodsList.slice(1, zctjGoodsList.length)//剩下的
        })
      })
      //首页 甜品自由
      ports.ModuleArticle.getNavigatorReleaseList({
        navigatorID: app.tianpinZyId,
        pageNumber: 5,
        objectDefineID: app.goodsobjectDefineID
      }, that, res => {
        var tpGoodsList = res.data.body.data.rows;
        this.setData({
          tpGoodsListfirst: tpGoodsList[0],//第一个,
          tpGoodsListOthers: tpGoodsList.slice(1, tpGoodsList.length)//剩下的
        })
      })
    },
    //隐藏
    hide_black: function (e) {
      this.setData({
        toggleGoodsFeature: false
      })
    },
    //加入购物车，立即购买动画
    setSubmitText: function (e) {
      var that = this
      that.setData({
        submittext: e.currentTarget.dataset.todo == '1' ? true : false,
        amount: 1
      })
      var id = e.currentTarget.dataset.id
      var memberID = wx.getStorageSync('USER_MEMBERID')
      if (id) {
        that.setData({
          goodsShopID: id
        })
        if (memberID != null) {
          that.getGoodsShopDetail(memberID)
        } else {
          wx.showToast({
            title: '请登录',
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: '参数异常',
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack()
        }, 1000)
      }
    },
    //商品详情
    toGoodsDetail(e) {
      wx.navigateTo({
        url: '/pages/proInfo/proInfo?id=' + e.currentTarget.dataset.id,
      })
    },
    //商品详情
    getGoodsShopDetail: function (memberID) {
      var that = this
      //店铺商品详情
      ports.ModuleGoodsShop.getGoodsShopDetail({
        goodsShopID: that.data.goodsShopID,
        type: 2,
        memberID: memberID
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            goodsShop: res.data.body.data,
            gshowPrice: res.data.body.data.realPrice
          })
          if (res.data.body.data.isCollect == 0) {
            that.setData({
              collected: false
            })
          } else {
            that.setData({
              collected: true
            })
          }
          that.getFeatureList(that.data.goodsShopID);
        }
      })

      //获取订单运费
      ports.ModuleGoodsShop.getParameterValueByCode({
        code: app.orderFare,
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            orderFare: res.data.body.data
          })
        }
      })
      //获取包邮信息
      ports.ModuleGoodsShop.getParameterValueByCode({
        code: app.orderAmountFreeFare,
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            orderAmountFreeFare: res.data.body.data
          })
        }
      })
    },
    //获取商品规格
    getFeatureList(objectID) {
      var that = this
      //获取产品规格   
      ports.ModuleGoodsShop.getObjectFeatureItemList({
        objectID: objectID,
        pageNumber: 999
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            featureItemList: res.data.body.data
          })
          if (that.data.featureItemList.length > 0) {
            that.setData({
              //默认选中第一个规格
              fitemName: that.data.featureItemList[0].itemList[0].objectFeatureItemName,
              fid: that.data.featureItemList[0].itemList[0].objectFeatureItemID
            })
          }
          that.setData({
            toggleGoodsFeature: true,
            showpromodel: true
          })
        }
      })
    },
    //获取红包
    getCanGetBonusDefineList() {
      var that = this
      var sessionID = wx.getStorageSync('USER_SESSIONID')
      if (sessionID) {
        ports.ModuleBonus.getCanGetBonusDefineList({}, that, res => {
          if (res.data.header.code == 0) {
            that.setData({
              bonusDefineList: res.data.body.data.rows
            })
          }

        })
      }
    },
    //领取红包
    getBonus(e) {
      var that = this
      var sessionID = wx.getStorageSync('USER_SESSIONID')
      var bonusDefineID = e.currentTarget.dataset.id
      var myStatus = e.currentTarget.dataset.status
      var canGetNum = e.currentTarget.dataset.cangetnum 
      var index = e.currentTarget.dataset.index
      if (sessionID) {
        if (myStatus == 0) {
          if (canGetNum > 0) {
            ports.ModuleBonus.memberGetBonus({
              bonusDefineID: bonusDefineID
            }, that, res => {
              if (res.data.header.code == 0) {
                wx.showToast({
                  title: "领取成功"
                })
                var bonusDefineList = that.data.bonusDefineList
                bonusDefineList[index].canGetNumber--
                bonusDefineList[index].myGetStatus = 1
                that.setData({
                  bonusDefineList
                })
              }else{
                wx.showToast({
                  title: res.data.body.msg,
                  icon:'none'
                })
              }
            })
          } else {
            wx.showToast({
              title: "领取次数用完啦！",
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: "不可重复领取！",
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: "请登录",
          icon: 'none'
        })
      }
    },
    onPullDownRefresh() {
      var that = this;
      ports.ModuleAll.PullDownRefresh(that, res => {
        that.load()
      })
    },
    //微信分享
    onShareAppMessage(res) {
      var memberID = wx.getStorageSync("USER_MEMBERID")
      var url = app.defaultShareUrl
      if (memberID) {
        url += "?memberID=" + memberID
      }
      return {
        title: app.defaultShareTitle,
        path: url,
        imageUrl: app.logoUrl
      }
    }
  },
  //自定义tabbar
  pageLifetimes: {
    show() {
      var that = this
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }
      that.getCanGetBonusDefineList()
      that.enterAni()
    },
    hide() {
      var that = this
      that.outAni()
    }
  }
})

